<?php
/**
 * Created by PhpStorm.
 * User: toshiba
 * Date: 2015/4/17
 * Time: 15:26
 */

class cart extends userBase {
    /** @var CartMode  */
    protected $cart;
    function __construct() {
        parent::__construct();
        $this->cart=CartMode::init();
    }

    function indexTask(){
        $result=array();
        View::displayAsHtml($result, "cart.php");
    }

    // 获取购物车清单
    function listTask(){
        $cartData=$this->cart->getList($this->user->getLoginId());
        $gopen_ids=array_map(function($v){
            return $v['gopen_id'];
        },$cartData);
        $goodsMode=GoodsMode::init();
        $goodsData=$goodsMode->getDetailInfo($gopen_ids);
        $cartDataIndex=array();
        foreach($cartData as $v){
            $cartDataIndex[$v['gopen_id']]=$v;
        }
        foreach($goodsData as &$v){
            $v['cart']=array(
                "cart_id"=>$cartDataIndex[$v['gopen_id']]['id'],
                "user_id"=>$cartDataIndex[$v['gopen_id']]['user_id'],
                "attr_id"=>$cartDataIndex[$v['gopen_id']]['attr_id'],
                "number"=>$cartDataIndex[$v['gopen_id']]['number'],
                "attr_name"=>$cartDataIndex[$v['gopen_id']]['attr_name'],
            );
        }
        $r['data']['list']=$goodsData;
        $r['data']['total']=count($goodsData);
        $r['state']=200;
        View::displayAsJson($r);
    }

    // 添加商品
    function add($gopen_id, $attr_id, $number= 1){
        $userId=$this->user->getLoginId();
        if($this->cart->checkExist($userId, $gopen_id)){
            return new DataMessage(DataMessage::STATE_ERROR,"要添加的商品已存在");
        }
        $goodsMode=GoodsMode::init();
        $goodsData=$goodsMode->getDetailWithoutJoin($gopen_id);
        if(empty($goodsData)){
            return new DataMessage(DataMessage::STATE_ERROR,"该商品不存在");
        }
        if($goodsData['type']!='normal'){
            return new DataMessage(DataMessage::STATE_ERROR,"该类型商品无法添加");
        }
        //如果没有属性
        if(!isset($attr_id) || empty($attr_id)){
            $attrList = $goodsMode->getAttrList($gopen_id);
            //var_dump($attrList); 
            $attr_id = $attrList[0]['id'];
        }
        //
        $attrData=$goodsMode->getAttrDetail($attr_id);
        if(!isset($attrData['gopen_id'])|| empty($attrData['gopen_id'])|| $attrData['gopen_id']!=$gopen_id){
            return new DataMessage(DataMessage::STATE_ERROR,"该商品没有此属性");
        }
        if($this->cart->add($userId, $gopen_id, $attr_id, $number)){
            return new DataMessage(DataMessage::STATE_SUCCESS, "添加成功");
        }else{
            return new DataMessage(DataMessage::STATE_ERROR, "添加失败");
        }
    }
    function addTask($gopen_id, $attr_id, $number= 1){
        $re= $this->add($gopen_id, $attr_id, $number);
        View::displayDataMessage($re);
    }

    // 修改购物车里面的内容
    function update($cart_id, $attr_id, $number){
        $userId=$this->user->getLoginId();
        $cartData=$this->cart->getList($userId, (int)$cart_id);
        if(empty($cartData)){
            return new DataMessage(DataMessage::STATE_ERROR,"要修改的cart_id不存在");
        }
        $cartData=$cartData[0];
        $goodsMode=GoodsMode::init();
        $attrData=$goodsMode->getAttrDetail($attr_id);
        if(!isset($attrData['gopen_id'])|| empty($attrData['gopen_id'])|| $attrData['gopen_id']!=$cartData['gopen_id']){
            return new DataMessage(DataMessage::STATE_ERROR,"该商品没有此属性");
        }
        if(!$this->cart->checkNumValid($attr_id, $number)){
            return new DataMessage(DataMessage::STATE_ERROR,"商品数量超过库存量");
        }
        if($this->cart->update($cart_id, $attr_id, $number)){
            return new DataMessage(DataMessage::STATE_SUCCESS,"修改成功");
        }else{
            //return new DataMessage(DataMessage::STATE_INFO,"修改失败，无修改");
            //为了前端方便显示
            return new DataMessage(DataMessage::STATE_SUCCESS,"无修改");
        }
    }
    function updateTask($cart_id, $attr_id, $number=1){
        $re=$this->update($cart_id, $attr_id, $number);
        View::displayDataMessage($re);
    }

    // 删除购物车商品，参数是id或者id数组
    function removeTask($cart_id){
        $userId=$this->user->getLoginId();
        $cartData=$this->cart->getList($userId, $cart_id);
        if(empty($cartData)){
            $re= new DataMessage(DataMessage::STATE_ERROR,"要修改的cart_id不存在");
        }elseif($this->cart->remove($cart_id)){
            $re= new DataMessage(DataMessage::STATE_SUCCESS, "删除成功");
        }else{
            $re= new DataMessage(DataMessage::STATE_ERROR, "删除失败");
        }
        View::displayDataMessage($re);
    }
} 